package com.biodiv.miracle.boot.auto;

import com.biodiv.miracle.support.annotation.ConditionalOnMissingBean;
import com.biodiv.miracle.boot.core.RedisTemplate;
import com.biodiv.miracle.support.annotation.Configuration;
import com.biodiv.miracle.support.utils.ResourceUtils;
import jodd.util.StringUtil;


import java.util.Properties;

/**
 * @author: tianquan
 * @date: 2025-05-28  15:58
 * @description: RedisTemplate自动装配
 */
@Configuration
public class RedisTemplateAutoConfiguration {

    private final String localHost;

    private final String port;

    private final String password;

    public RedisTemplateAutoConfiguration() {
        Properties properties = ResourceUtils.getProperties();
        this.localHost = properties.getProperty("redis.localhost");
        this.port = properties.getProperty("redis.port");
        this.password = properties.getProperty("redis.password");
    }

    @ConditionalOnMissingBean
    public RedisTemplate redisTemplate() {
        RedisTemplate redisTemplate = null;
        if (StringUtil.isNotBlank(localHost) && StringUtil.isNotBlank(port)) {
            redisTemplate = new RedisTemplate(localHost, Integer.parseInt(port), password);
        }
        return redisTemplate;
    }

}
